package wells.leetcode.day20200723;

public class Solution {

	public static void main(String[] args) {
		int[][] grid = new int[][] { { 1, 3, 1 }, { 1, 5, 1 }, { 4, 2, 1 } };

		new Solution().minPathSum(grid);
	}

	public int minPathSum(int[][] grid) {

		for (int i = 0; i < grid.length; i++) {
			int[] row = grid[i];
			for (int j = 0; j < row.length; j++) {
				if (j == 0) {
					if (i > 0) {
						grid[i][j] = grid[i][j] + grid[i - 1][j];
					}
				} else {
					if (i == 0) {
						grid[i][j] = grid[i][j] + grid[i][j - 1];
					} else {
						grid[i][j] = grid[i][j] + Math.min(grid[i - 1][j], grid[i][j - 1]);
					}
				}
			}
		}

		int[] lastRow = grid[grid.length - 1];
		return lastRow[lastRow.length - 1];

	}
}
